<?php
/**
 * 插件管理 - Ora 模块
 * @author lan7
 * @copyright lan7.net
 * @version 2.0
 */
class admin_plugin extends mod{

	/**
	 * 插件管理模块类 初始化
	 *
	 */
	function __construct(){
		parent::__construct();
		session_start();
		$this->setTplDir(PUBLIC_PATH);
		$this->setMsgTpl("admin/msg.html");
		if(!isset($_SESSION[$GLOBALS['ora_cfg']['sess_prefix']."admin"])){
			$this->showMsg("请先登录!",parse_uri("common-login"),1);
		}
	}

	/**
	 * 插件管理首页
	 *
	 */
	function index(){
		import("lib.plugin");
		$myps = array();
		$q = $this->db->query("select * from ".tn('plugin')." order by Id desc");
		while($row = $this->db->fetch_array($q)){
			$myps[$row['p_code']] = $row;
		}
		$this->assign("myps",$myps);
		$this->assign('pKeys',array_keys($myps));
		$this->assign("plugins",plugin_lib::getAllPlugin());
		$this->display("admin/plugin.html");
	}

	/**
	 * 插件安装
	 *
	 */
	function add(){
		if(isset($_POST['submit'])){
			import("lib.upload");
			$up = new Upload("zip");
			if($up->errInfo){
				$this->showMsg($up->errInfo,parse_uri('admin_plugin-index'),1);
			}
			$fileName = str_replace(".".$up->files[0]["ext"],"",$up->files[0]["name"]);
			$zip = "zip/plugin_".$fileName.".zip";
			@rename(ROOT_PATH."upload/".$up->files[0]["urlPath"],ROOT_PATH."upload/".$zip);
			import("lib.plugin");
			if(!plugin_lib::validate($fileName,$zip)){
				$this->showMsg("插件结构不合法",parse_uri("admin_plugin-add"));
			}
			if(plugin_lib::install($fileName)){
				$this->showMsg("添加成功!",parse_uri('admin_plugin-index'),1);
			}else{
				$this->showMsg("添加失败!",parse_uri('admin_plugin-index'),1);
			}
		}
		$this->display("admin/plugin.html");
	}

	/**
	 * 在线获取插件包并安装.
	 *
	 */
	function installol(){
		$pn = $GLOBALS['_param']['pn'];
		if(preg_match('/^[a-zA-Z]+\w*?$/i',$pn)){
			import("lib.update");
			import("lib.plugin");
			$fileSize = update_lib::getRemoteFileSize(ORA_UPDATE."update/getPlugin/key_".base64_encode($pn));
			$this->assign("key",$pn);
			$this->assign("fileSize",$fileSize);
			$this->assign("plugins",plugin_lib::getAllPlugin());
			$this->display("admin/plugin.html");
		}
		$this->showMsg("错误的插件代码");
	}

	/**
	 * 下载插件
	 *
	 */
	function doInstall(){
		$key = base64_decode($GLOBALS['_param']["key"]);
		if($key){
			import("lib.update");
			import("lib.plugin");
			$f = update_lib::getFile(ORA_UPDATE."update/getPlugin/key_".base64_encode($key),PLUGIN_PATH."plugin");
			if(unpackZip(PLUGIN_PATH.$key."/",$f)){
				if(!plugin_lib::install($key,true)){
					exit("fail");
				}
			}else{
				//unzip error
				exit("fail");
			}
		}
	}

	/**
	 * 删除进度条临时文件
	 *
	 */
	function delNow(){
		io::write(UPLOAD_PATH."/now","0");
	}


	/**
	 * 插件安装
	 * @param $rs 是否重新安装
	 * @param $p_code 插件Code
	 */
	function install(){
		$rs = isset($GLOBALS['_param']['rs'])?true:false;
		$p_code = $GLOBALS['_param']['pc'];
		import("lib.plugin");
		if(plugin_lib::install($p_code,$rs)){
			$this->showMsg("安装成功!",parse_uri('admin_plugin-index'),1);
		}
		$this->showMsg("安装失败!",parse_uri('admin_plugin-index'),1);
	}

	/**
	 * 插件卸载
	 * @param $p_code 插件Code
	 */
	function uninstall(){
		$p_code = $GLOBALS['_param']['pn'];
		import("lib.plugin");
		if(plugin_lib::uninstall($p_code)){
			$this->showMsg("卸载成功!",parse_uri('admin_plugin-index'),1);
		}
		$this->showMsg("卸载失败!",parse_uri('admin_plugin-index'),1);
	}

	/**
	 * 添加菜单项至左侧管理菜单
	 *
	 */
	function addMenu(){
		$p_code = $GLOBALS['_param']['pn'];
		if(preg_match('/[a-zA-Z]+\w*?/i',$p_code)){
			$p = $this->db->getOne("select * from ".tn('plugin')." where p_code = '$p_code' ");
			import("lib.plugin");
			if(plugin_lib::addQuickStart($p_code,$p['p_name'])){
				$this->showMsg("操作成功!<script>window.parent['leftFrame'].location.reload();</script>");
			}
		}
		$this->showMsg("操作失败");
	}

	/**
	 * 插件项管理
	 *
	 */
	function mgr(){
		$p_code = $GLOBALS['_param']['pn'];
		if(preg_match('/[a-zA-Z]+\w*?/i',$p_code)){
			import("lib.plugin");
			plugin_lib::admin($p_code);
		}
	}

	/**
	 * 添加至网站导航
	 *
	 */
	function addNav(){
		$p_code = $GLOBALS['_param']['pn'];
		if(preg_match('/[a-zA-Z]+\w*?/i',$p_code)){
			$p = $this->db->getOne("select * from ".tn('plugin')." where p_code = '$p_code' ");
			import("lib.plugin");
			if(plugin_lib::addNav($p_code,$p['p_name'])){
				$this->showMsg("操作成功!");
			}
		}
		$this->showMsg("操作失败");
	}

}
?>